package m.jclecai.com.pay;

import android.text.TextUtils;

import com.iapppay.interfaces.callback.IPayResultCallback;
import com.iapppay.sdk.main.IAppPay;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;

import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.PayChannelBean;
import m.jclecai.com.bean.Response.ResAiBeiPay;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.ChannelUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;


/**
 * Created by Dinosa on 2017/12/29.
 * 爱贝的充值渠道
 */

public class AiBeiPayImpl extends BasePayImpl {

    private  Callback mCallback=null;
    public BaseActivity mActivity;
    public String mUserId;
    public String mPayMoney;

    /**
     * 带回掉的支付
     * @param activity
     * @param userId
     * @param callback
     */
    public AiBeiPayImpl(BaseActivity activity, String userId, Callback callback) {
        super(activity);
        mActivity = activity;
        mUserId = userId;
        mCallback = callback;
    }
    public AiBeiPayImpl(BaseActivity activity, String userId) {
        super(activity);
        mActivity = activity;
        mUserId = userId;
    }


    /**
     * 爱贝聚合的支付方式；
     * @param checkedBean
     */
    private void payWidthAiBei(PayChannelBean checkedBean) {
        //爱贝的地址；
        String aiBeiUrl="payment/iapppay_android/order.php";
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("pay_id",mUserId);
        paramsMap.put("price",mPayMoney);
        paramsMap.put("channel",checkedBean.getChannel());
        paramsMap.put("appstore", ChannelUtil.getChannel());

        HttpManager.postAsyn(aiBeiUrl, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                e.printStackTrace();
                mActivity.DisPlay("网络异常");
                dissDialog();
            }
            @Override
            public void onResponse(String response) {
                dissDialog();
                if (!TextUtils.isEmpty(response)) {
                    //这里我们启动爱贝的adk
                    ResAiBeiPay resAiBeiPay = FastOk.get(response, ResAiBeiPay.class);
                    String params="transid="+resAiBeiPay.getTransid()+"&"+"appid=3015703585" ;
                    IAppPay.startPay(mActivity, params, new IPayResultCallback() {
                        @Override
                        public void onPayResult(int resultCode, String s, String s1) {
                            //这里是之后的回掉内容；
                            //这里处理爱贝支付的回掉信息
                            if(resultCode == IAppPay.PAY_SUCCESS){
                                //成功
                                DisPlay("充值成功");
                                if (mCallback != null) {
                                    mCallback.callbackSuccess();
                                }
                            }else if(resultCode == IAppPay.PAY_ERROR){
                                //错误
                                DisPlay("充值异常");
                                if (mCallback != null) {
                                    mCallback.callbackField();
                                }
                            }else if(resultCode == IAppPay.PAY_CANCEL){
                                //取消；
                                DisPlay("充值取消");
                                if (mCallback != null) {
                                    mCallback.callbackField();
                                }
                            }
                        }
                    });
                }
            }
        },paramsMap);
    }

    @Override
    public void pay(PayChannelBean channelBean, String payMoney) {
        mPayMoney=payMoney;
        showDialog();
        payWidthAiBei(channelBean);
    }



}
